﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entity;

namespace Web.admin.UserManage
{
    public partial class Privileges : System.Web.UI.Page
    {
        private ModuleService moduleService = new ModuleService();
        private RoleService roleService = new RoleService();
        private string roleId = "";
        private List<ModuleVo> checkedModules = new List<ModuleVo>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                roleId = Request["RoleId"];
                if (String.IsNullOrEmpty(roleId))
                {
                    return;
                }

                BoRoles role = roleService.getRole(roleId);
                if (role == null)
                    return;

                RoleName.Text = role.ROL_NAME;
                RoleIdBox.Value = roleId;

                InitModuleTree();
            }
        }

        protected void InitModuleTree()
        {
            ModuleVo module = moduleService.getAllModules();
            List<ModuleVo> roleModules = roleService.getAuthorizedModules(roleId);

            ModuleTree.Nodes.Clear();

            TreeNode rootNode = MakeTreeNode(module, roleModules);
            ModuleTree.Nodes.Add(rootNode);
        }

        protected TreeNode MakeTreeNode(ModuleVo moduleVo, List<ModuleVo> authorizedModules)
        {
            TreeNode node = new TreeNode();
            node.Text = moduleVo.DisplayName;
            node.Value = moduleVo.ModuleID;
            node.NavigateUrl = moduleVo.ModuleURL;
            node.Target = "mainFrame";
            foreach (ModuleVo v in authorizedModules)
            {
                if (moduleVo.ModuleID == v.ModuleID)
                {
                    node.Checked = true;
                    break;
                }
            }

            foreach (ModuleVo child in moduleVo.Children)
            {
                TreeNode childNode = MakeTreeNode(child, authorizedModules);
                node.ChildNodes.Add(childNode);
            }
            return node;
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            checkedModules.Clear();
            RetrieveCheckedModules(ModuleTree.Nodes[0]);
            RoleVo rv = new RoleVo();
            rv.RoleId = RoleIdBox.Value;
            rv.RoleModules = checkedModules;
            rv.SaveRoleModules();

            Response.Redirect("./UserRole/List.aspx");
        }

        protected bool RetrieveCheckedModules(TreeNode node)
        {
            foreach (TreeNode child in node.ChildNodes)
            {
                if (RetrieveCheckedModules(child))
                    node.Checked = true;
            }

            if (node.Parent != null && node.Checked)
            {
                ModuleVo mv = new ModuleVo();
                mv.ModuleID = node.Value;
                checkedModules.Add(mv);
            }

            return node.Checked;
        }
    }
}